package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.CountsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UserLikeService;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserService userService;

    @Autowired
    private UserLikeService userLikeService;

    /**
     * 通过id查询用户详情
     *
     * @param userID
     * @param
     * @return
     */
    @GetMapping
    public ResponseEntity users(Long userID) {
        if (userID == null) {
            userID = UserHolder.getUserId();
        }
        //2,调用service查询
        UserInfoVo userInfoVo = userInfoService.getById(userID);
        return ResponseEntity.ok(userInfoVo);
    }


    /**
     * 修改用户详情
     *
     * @param userInfo
     * @return
     */
    @PutMapping
    public ResponseEntity update(@RequestBody UserInfo userInfo) {
        userInfo.setId(UserHolder.getUserId());
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * 发送短信验证码
     *
     * @return
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode() {
        //获取手机号
        String mobile = UserHolder.getMobile();
        //调用service发送验证码
        userService.SendMsg(mobile);
        return ResponseEntity.ok(null);
    }

    /**
     * 校验验证码
     *
     * @param map
     * @return
     */
    @PostMapping("phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map) {
        String verificationCode = (String) map.get("verificationCode");
        //获取手机号
        String mobile = UserHolder.getMobile();
        //调用service校验验证码
        Boolean flag = userService.checkVerificationCode(mobile, verificationCode);
        return ResponseEntity.ok(flag);
    }

    /**
     * 保存新手机号
     *
     * @param map
     * @return
     */
    @PostMapping("/phone")
    public ResponseEntity phone(@RequestBody Map map) {
        String phone = (String) map.get("phone");
        //调用service修改手机号
        userService.phone(phone);
        return ResponseEntity.ok(null);
    }

    /**
     * 互相喜欢，喜欢，粉丝统计
     *
     * @return
     */
    @GetMapping("/counts")
    public ResponseEntity counts() {
        CountsVo countsVo = userLikeService.counts();
        return ResponseEntity.ok(countsVo);
    }

    /**
     * @param type 类型  1 互相关注 2 我关注 3 粉丝 4 谁看过我
     * @param page 页数
     * @param pagesize 每页几条
     * @param nickname 昵称
     * @return
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity typeSum(@PathVariable("type") String type,
                                  @RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "10") Integer pagesize,
                                  String nickname) {
        PageResult pr = userLikeService.typeSum(type,page,pagesize,nickname);
        return ResponseEntity.ok(pr);
    }
}
